package com.newbingo.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.newbingo.dao.imple.IOperatorDao;
import com.newbingo.dto.imple.IOperatorDto;
import com.newbingo.entity.Operator;
import com.newbingo.service.imple.IOperatorService;
import com.newbingo.util.Pager;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.*;

public class OperatorDto extends UserDto implements IOperatorDto, Serializable {

	private Integer id;
	private Integer agentId;
	private String loginName;
	private String trueName;
	private String email;
	private String tel;
	private String lastLoginTime;
	private String regTime;
	private String status;
	private String lastLoginIp;

	private IOperatorDao operatorDao = null;
	private IOperatorService operatorService=null;

	/*
	 * 查询 转换成DTO 返回Pager对象(non-Javadoc)
	 * 
	 * @see com.newbingo.dto.imple.IOperatorDto#findOperator(int, int,
	 *      java.lang.Integer)
	 */
	public Pager findOperator(int start, int limit, Integer agentId) {
		Pager pager = new Pager();
		List<OperatorDto> operatorDto = new ArrayList<OperatorDto>();
		OperatorDto op = null;
		Pager page = operatorDao.findOperator(start, limit, agentId);
		List<Operator> list = page.getData();
		for (Operator operator : list) {
			op = new OperatorDto();
			op.setId(operator.getUno());
			op.setAgentId(operator.getAgent().getUno());
			op.setLoginName(operator.getUname());
			op.setTrueName(operator.getTrueName());
			op.setEmail(operator.getContactInfo().getEmail());
			op.setTel(operator.getContactInfo().getTel());
			op.setLastLoginIp(operator.getLastLoginIp());
			op.setStatus(operator.getStatus());
			if (operator.getLastLogTime() != null) {
				op.setLastLoginTime(operator.getLastLogTime() + "");
			} else {
				op.setLastLoginTime("");
			}
			if (operator.getRegTime() != null) {
				op.setRegTime(operator.getRegTime() + "");
			} else {
				op.setRegTime("");
			}
			operatorDto.add(op);
		}
		pager.setData(operatorDto);
		return pager;
	}

	public OperatorDto findById(Integer Id) {
		Operator operator = operatorDao.findById(Id);
		OperatorDto op = new OperatorDto();
		op.setId(operator.getUno());
		op.setAgentId(operator.getAgent().getUno());
		op.setLoginName(operator.getUname());
		op.setTrueName(operator.getTrueName());
		op.setEmail(operator.getContactInfo().getEmail());
		op.setTel(operator.getContactInfo().getTel());
		op.setLastLoginIp(operator.getLastLoginIp());
		op.setStatus(operator.getStatus());
		if (operator.getLastLogTime() != null) {
			op.setLastLoginTime(operator.getLastLogTime() + "");
		} else {
			op.setLastLoginTime("");
		}
		if (operator.getRegTime() != null) {
			op.setRegTime(operator.getRegTime() + "");
		} else {
			op.setRegTime("");
		}
		return op;
	}

	public OperatorDto findByLogin(String uname,String upwd){
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getRemoteAddr();
		Operator operator=operatorDao.findByLogin(uname, upwd);
		Date date=new Date();
		if (operator != null) {
			operator.setLastLoginIp(ip);
			operator.setLastLogTime(date);
			operatorService.merge(operator);
		} 
		OperatorDto op = new OperatorDto();
		op.setId(operator.getUno());
		op.setAgentId(operator.getAgent().getUno());
		op.setLoginName(operator.getUname());
		op.setTrueName(operator.getTrueName());
		op.setEmail(operator.getContactInfo().getEmail());
		op.setTel(operator.getContactInfo().getTel());
		op.setLastLoginIp(operator.getLastLoginIp());
		op.setStatus(operator.getStatus());
		if (operator.getLastLogTime() != null) {
			op.setLastLoginTime(operator.getLastLogTime() + "");
		} else {
			op.setLastLoginTime("");
		}
		if (operator.getRegTime() != null) {
			op.setRegTime(operator.getRegTime() + "");
		} else {
			op.setRegTime("");
		}
		return op;
	}
	
	
	public IOperatorDao getOperatorDao() {
		return operatorDao;
	}

	public void setOperatorDao(IOperatorDao operatorDao) {
		this.operatorDao = operatorDao;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getAgentId() {
		return agentId;
	}

	public void setAgentId(Integer agentId) {
		this.agentId = agentId;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(String lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public String getRegTime() {
		return regTime;
	}

	public void setRegTime(String regTime) {
		this.regTime = regTime;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public IOperatorService getOperatorService() {
		return operatorService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}

}
